<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<script type="text/javascript" src="components/com_meupload/uploadify/jquery-1.4.2.min.js"></script>
<?php
JToolBarHelper::cancel();
$post = JRequest::get('post');
if(empty($post['step']))
{
    ?>
    <script type="text/javascript">

    jQuery.noConflict();
    (function($) {
        $(function() {
            $('#addb').click(function(){
                var location = $('#location').val();
                if(location==''){
                    alert('You must to input your folder name.');
                    $('#location').focus();
                    return false;
                }
                else{
                    var patt1=/\W/;
                    if(patt1.test(location)==true){
                        alert('Do not use white space or special character with folder');
                        $('#location').focus();
                        return false;
                    }
                    
                }
            });
            
        });
    })(jQuery);


    </script>
    <form id="adminForm" name="adminForm" method="post" action="index.php" autocomplete="off">

        <label for="location">Folder name:</label>
        <input type="text" id="location" name="cid[]" value="" />
        <input type="submit" value="Add folder" id="addb">
        
        <input type="hidden" name="option" value="com_meupload" />
        <input type="hidden" name="step" value="2" />
        <input type="hidden" name="task" value="add" />
        <input type="hidden" name="view" value="form" />
        <?php echo JHTML::_( 'form.token' ); ?>
    </form>
    <?php
}
else
{
    JToolBarHelper::deleteList();
    $name = $post['cid'][0];
?>
    <link href="components/com_meupload/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="components/com_meupload/uploadify/swfobject.js"></script>
    <script type="text/javascript" src="components/com_meupload/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
    <script type="text/javascript">

    jQuery.noConflict();
    (function($) {
        $(function() {
            // more code using $ as alias to jQuery
            $('#file_upload').uploadify({
                'uploader'  : 'components/com_meupload/uploadify/uploadify.swf',
                'script'    : 'components/com_meupload/uploadify/uploadify.php',
                'cancelImg' : 'components/com_meupload/uploadify/cancel.png',
                'folder'    : '../images/stories/<?php echo $name ?>',
                'fileExt'   : '*.jpg;*.gif;*.png;*.jpeg',
                'fileDesc'  : 'All',
                'auto'      : false,
                'multi'     : true
            });

            $('#upload_files').click(function(){
                $('#file_upload').uploadifyUpload();
                return false;
            });

            $('#cancel_files').click(function(){
                $('#file_upload').uploadifyClearQueue();
                return false;
            });


        });
    })(jQuery);

    function isChecked(isitchecked){
        if (isitchecked == true){
            document.adminForm.boxchecked.value++;
        }
        else {
            document.adminForm.boxchecked.value--;
        }
    }

    function submitbutton(button){
        if(button=='remove'){
            if(confirm('Do you want to delete image?')===false){
                return false;
            }else{
                submitform(button);
            }
        }else{
            submitform(button);
        }
    }
    </script>
    <form id="adminForm" name="adminForm" method="post" action="index.php">
        <div style="width:35%; float:left;">
            <label for="location">Folder name:</label>
            <input type="text" id="location" name="location" value="<?php echo $name?>" disabled/><br /><br />

            <div id="upload_bar">
                <input id="file_upload" name="file_upload" type="file" />
                <a href="#" id="upload_files" >Upload Files</a> | <a href="#" id="cancel_files" >Cancel File</a>
            </div>
            
            <?php echo JHTML::_( 'form.token' ); ?>
        </div>
        <div id="display_listing" style="width:45%; float:left;">
            <?php
            $path = JPATH_ROOT.DS.'images'.DS.'stories'.DS.$name;
            $files_list = Folderlist::showfiles($path);
            if($files_list!==null)
            {
                $uri = new JURI();
                $img = $uri->root().'images'.DS.'stories'.DS.$name;
                foreach($files_list AS $file){
                    echo '<div class="img_cover">';
                    echo '<img class="cImg" alt="'.$file.'" src="'.$img.DS.$file.'" />';
                    echo '<input type="checkbox" name="cid[]" id="'.$file.'" value="'.$file.'" onclick="isChecked(this.checked);" />';
                    echo '<label class="lable_img" for="'.$file.'">Selected</label>';
                    echo '</div>';
                }
            }
            

            ?>
            <input type="hidden" name="task" value="" />
            <input type="hidden" name="option" value="com_meupload" />
            <input type="hidden" name="del_path" id="del_path" value="<?php echo $path ?>" />
            <input type="hidden" name="boxchecked" value="0">
        </div>
    </form>
<?php
}
?>